const db = require('../db/index')
const { date } = require('../uitls/dateFormat')



exports.getinfoById = (req, res) => {
    //查询所有信息
    const sql = 'select * from movieinfo where id = ?'
    db.query(sql, req.params.id, (err, results) => {
        // 执行 SQL 语句失败
        if (err) {
            return res.cc(err.message)
        }
        const movieinfo = results[0]
            //查询总条数
        const sql = 'select count(*) as total from comments where refid = ?'
        db.query(sql, req.params.id, (err, results) => {
            // 执行 SQL 语句失败
            if (err) {
                return res.cc(err.message)
            }
            const total = results[0].total
                //查询电影的评论
            const sql = 'select * from comments where refid = ?'
            db.query(sql, req.params.id, (err, results) => {
                // 执行 SQL 语句失败
                if (err) {
                    return res.cc(err.message)
                }
                const comments = results
                res.send({
                    status: 200,
                    message: "获取成功",
                    data: {
                        movieinfo,
                        total,
                        comments: comments || []
                    }
                })
            })
        })
    })
}

exports.getAll2 = (req, res) => {
    const sql = 'select * from movieinfo order by id desc'
    db.query(sql, (err, results) => {
        // 执行 SQL 语句失败
        if (err) {
            return res.cc(err.message)
        }
        res.send({
            status: 200,
            message: '获取电影信息成功',
            data: results
        })
    })
}


//获取全部电影
exports.getAll = (req, res) => {
    const sql = 'select * from movieinfo LIMIT 5'
    db.query(sql, (err, results) => {
        // 执行 SQL 语句失败
        if (err) {
            return res.cc(err.message)
        }
        res.send({
            status: 200,
            message: '获取电影信息成功',
            data: results
        })
    })
}

//添加电影
exports.addMovie = (req, res) => {
    const movieinfo = req.body.movieinfo
    const sql = 'insert into movieinfo set ?'
    db.query(sql, {
        addtime: date(new Date()),
        name: movieinfo.name,
        category: movieinfo.category,
        posters: movieinfo.posters,
        director: movieinfo.director,
        starring: movieinfo.starring,
        price: movieinfo.price,
        Introduction: movieinfo.Introduction
    }, (err, results) => {
        // 执行 SQL 语句失败
        if (err) {
            return res.cc(err.message)
        }
        if (results.affectedRows !== 1) {
            return res.cc('添加失败！')
        }
        // 注册成功
        res.send({
            status: 200,
            message: '添加成功！',
        })
    })
}

//修改电影信息
exports.updateById = (req, res) => {
    const sql = `update movieinfo set name='${req.body.name}',category='${req.body.category}',
    posters='${req.body.posters}',director='${req.body.director}',starring='${req.body.starring}',
    price=${req.body.price},Introduction='${req.body.Introduction}' where id =${req.body.id}`
    db.query(sql, (err, results) => {
        if (err) return res.cc(err)
        res.send({
            status: 200,
            message: "修改电影信息成功"
        })
    })
}

//删除电影信息
exports.delMovie = (req, res) => {
    const sql = `delete from movieinfo where id = ${req.params.id}`
    db.query(sql, function(err, results) {
        if (err) return res.cc(err)
        res.send({
            status: 200,
            message: "删除电影信息成功！",
        })
    })
}